﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Caching;
using System.Collections;

namespace CommonTagTime
{

    public class clsCacheUtil
    {
        public static object GetCache(string key)
        {
            Cache cache = System.Web.HttpRuntime.Cache;
            return cache[key];
        }      
        public static void SetCache(string key, object val, CacheDependency depend)
        {
            Cache cache = System.Web.HttpRuntime.Cache;
            cache.Insert(key, val, depend);
        }
        
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="timeCountDown"></param>
        public static void SetCache(string key, object val)
        {
            Cache cache = System.Web.HttpRuntime.Cache;
            cache.Insert(key, val, null, DateTime.Now.AddSeconds(clsConfigData.CacheDuration), TimeSpan.Zero, CacheItemPriority.Default, null);
        }
        public static void RemoveCache(string key)
        {
            Cache cache = System.Web.HttpRuntime.Cache;
            cache.Remove(key);
        }
        public static void Reset()
        {
            Cache cache = System.Web.HttpRuntime.Cache;
            IDictionaryEnumerator ce = cache.GetEnumerator();
            while (ce.MoveNext())
                cache.Remove((string)ce.Key);
        }
        public static void RemoveCacheWithPrefix(string prefix)
        {
            Cache cache = System.Web.HttpRuntime.Cache;
            IDictionaryEnumerator ce = cache.GetEnumerator();
            while (ce.MoveNext())
            {
                if (ce.Key.ToString().StartsWith(prefix))
                    cache.Remove((string)ce.Key);
            }
        }
    }
}